package com.bestcem.xm.user.grpc.client;

import com.bestcem.xm.bi.grpc.v1.services.*;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.user.grpc.client.convert.UserBaseBiGrpcConvert;
import com.bestcem.xm.user.grpc.client.dto.BiShareDTO;
import com.bestcem.xm.user.grpc.client.dto.DashboardDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户bi相关基础grpc服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/29 14:54
 */
@Slf4j
@Service("userBaseBiGrpcService")
public class UserBaseBiGrpcService {

    @GrpcClient(GrpcClientNames.DS_BI)
    private BaseBiServiceGrpc.BaseBiServiceBlockingStub stub;

    @Resource
    private UserBaseBiGrpcConvert userBaseBiGrpcConvert;

    /**
     * 根据shareid获取share信息
     *
     * @param id shareId
     * @return com.bestcem.xm.user.grpc.client.dto.BiShareDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/29 15:05
     */
    public BiShareDTO getShareById(String id) {
        if (log.isInfoEnabled()) {
            log.info("根据shareid获取share信息参数：id:{}", id);
        }
        GetShareRequest.Builder builder = GetShareRequest.newBuilder();
        builder.setId(id);
        GetShareRequest request = builder.build();
        // 请求bi
        ShareResponse share = stub.getShare(request);
        // 校验查询结果
        if (Code.ERROR_VALUE == share.getStatus().getCodeValue()) {
            log.error("根据shareid获取share信息失败：dto:{} id:{}", share.getStatus().getMessage(), id);
            return null;
        }

        return userBaseBiGrpcConvert.responseToBiShareDTO(share.getShare());
    }

    /**
     * 根据dashboard的id获取dashboard信息
     *
     * @param id dashboardId
     * @return com.bestcem.xm.user.grpc.client.dto.DashboardDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/29 16:37
     */
    public DashboardDTO getDashboardById(String id) {
        if (log.isInfoEnabled()) {
            log.info("根据dashboard的id获取dashboard信息参数：id:{}", id);
        }
        GetDashboardRequest.Builder builder = GetDashboardRequest.newBuilder();
        builder.setId(id);
        GetDashboardRequest request = builder.build();
        // 请求bi
        DashboardResponse dashboard = stub.getDashboard(request);
        // 校验查询结果
        if (Code.ERROR_VALUE == dashboard.getStatus().getCodeValue()) {
            log.error("根据dashboard的id获取dashboard信息失败：dto:{} id:{}", dashboard.getStatus().getMessage(), id);
            return null;
        }

        return userBaseBiGrpcConvert.responseToDashboardDTO(dashboard.getDashboard());
    }

}
